package com.google.gac.masai.getit;

import java.io.File;
import java.net.URLConnection;
import java.util.Calendar;

import android.app.AlertDialog;
import android.app.ListFragment;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CursorAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class DownloadFragment extends ListFragment {
	private View mContentView;

	private static final String[] PROJECTION = new String[] {
			DownloadLog.Logs._ID, // 0
			DownloadLog.Logs.COLUMN_NAME_TITLE, // 1
			DownloadLog.Logs.COLUMN_NAME_URI, // 2
			DownloadLog.Logs.COLUMN_NAME_TAG, // 3
			DownloadLog.Logs.COLUMN_NAME_CREATE_DATE // 4
	};

	private static final int COLUMN_INDEX_TITLE = 1;
	private static final int COLUMN_INDEX_URI = 2;
	private static final int COLUMN_INDEX_LOG = 3;
	private static final int COLUMN_INDEX_CREATE_DATE = 4;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent intent = getActivity().getIntent();
		intent.setData(DownloadLog.Logs.CONTENT_URI);
		if (intent.getData() == null) {
			setListAdapter(new ArrayAdapter<String>(getActivity(),
					R.xml.list_item, new String[] { "No files downloaded" }));
		} else {

			/*
			 * Performs a managed query. The Activity handles closing and
			 * requerying the cursor when needed.
			 */
			Cursor cursor = getActivity().managedQuery(intent.getData(), // Use
																			// the
																			// default
																			// content
																			// URI
																			// for
																			// the
																			// provider.
					PROJECTION, // Return the note ID and title for each note.
					null, // No where clause, return all records.
					null, // No where clause, therefore no where column values.
					DownloadLog.Logs.DEFAULT_SORT_ORDER // Use the default sort
														// order.
					);
			Log.v("CURSOR", "" + cursor.getCount());
			if (cursor.getCount() == 0) {
				setListAdapter(new ArrayAdapter<String>(getActivity(),
						R.xml.list_item, new String[] { "No files downloaded" }));
			} else {
				String[] dataColumns = { DownloadLog.Logs.COLUMN_NAME_TITLE,
						DownloadLog.Logs.COLUMN_NAME_URI,
						DownloadLog.Logs.COLUMN_NAME_TAG,
						DownloadLog.Logs.COLUMN_NAME_CREATE_DATE };
				int[] viewIDs = { R.id.rowtext, R.id.rowtext2, R.id.rowtext3,
						R.id.rowtext4 };
				SimpleCursorAdapter adapter = new SimpleCursorAdapter(
						getActivity(), R.layout.downloadlist_item, cursor,
						dataColumns, viewIDs);
				setListAdapter(adapter);
			}
		}
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		// // Current position should survive screen rotations.
		//
		// SharedPreferences mPreferences;
		// mPreferences = PreferenceManager
		// .getDefaultSharedPreferences(getActivity());
		// int numDownloads;
		// numDownloads = mPreferences.getInt("numDownloads", 0);
		// String[] allDownloads;
		// if (numDownloads > 0)
		// allDownloads = new String[numDownloads];
		// else
		// allDownloads = new String[1];
		//
		// for (int i = 0; i < numDownloads; i++) {
		// allDownloads[i] = mPreferences.getString("download" + i, null);
		// }
		//
		// if (numDownloads == 0) {
		// allDownloads[0] = "No files downloaded.";
		// }
		//
		// // setListAdapter(new ArrayAdapter<String>(getActivity(),
		// // R.xml.list_header, new String[] {"Downloads"}));
		// // setListAdapter(new ArrayAdapter<String>(getActivity(),
		// R.xml.list_item,
		// // allDownloads));
		//
		// // ListView lv = getListView();
		// // lv.setTextFilterEnabled(true);
		//
		// // }
		// if (numDownloads == 0)
		// allDownloads[0] = "No files downloaded.";

	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		Cursor cursor = (Cursor) getListAdapter().getItem(position);
		//Log.i("DownloadFragment", "" +uri);
		Log.e("DownloadFragment", "" +cursor.getString(COLUMN_INDEX_URI));
		File fp = new File(cursor.getString(COLUMN_INDEX_URI));
		try{
			Intent i = new Intent();
			i.setAction(Intent.ACTION_VIEW);
			i.setDataAndType(Uri.fromFile(fp), URLConnection.guessContentTypeFromName(fp.getName()));
			startActivity(i);
			}
			catch(ActivityNotFoundException e) {
				AlertDialog alert=new AlertDialog.Builder(getView().getContext()).create();
				alert.setMessage("No program capable of opening that file was found.");
				alert.show();
			}
		super.onListItemClick(l, v, position, id);
	}

}
